<template>
  <div>
    <el-dialog
      title="居民评价"
      :visible.sync="addDiavisible"
      width="50%"
      @close="cancel"
      @opened="beforeopen"
    >
      <!-- 表格 -->
      <el-table class="mt-20" ref="tableRef" :data="tableData" border stripe>
        <el-table-column type="index" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="content" label="服务内容" align="center">
        </el-table-column>
        <el-table-column
          prop="performanceResult"
          label="履约进度"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="performanceTime"
          label="履约时间"
          align="center"
          :formatter="formatter1"
        >
        </el-table-column>
      </el-table>
      <el-form
        :model="param"
        ref="formRef"
        label-width="110px"
        label-position="left"
      >
        <el-row :gutter="20" class="rowSty">
          <el-col :lg="12" :xl="12">
            <el-form-item label="评分">
              <el-select v-model="param.level" style="width: 100%">
                <el-option
                  v-for="item in level"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :xl="12"> </el-col>
        </el-row>
        <el-form-item label="评价">
          <el-input v-model="param.content" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addComment, getComment } from "@/api/family_doctor/family_doctor";
export default {
  data() {
    return {
      addDiavisible: false,
      tableData: [],
      completeContent: {},
      param: {
        level: "",
        content: "",
        serviceContractId: "",
        servicePackageId: "",
      },
      level: [
        {
          id: "1",
          name: "一般",
        },
        {
          id: "2",
          name: "满意",
        },
        {
          id: "3",
          name: "非常满意",
        },
      ],
    };
  },
  created() {},
  methods: {
    //关闭
    cancel() {
      this.param.level = "";
      this.param.content = "";
      this.addDiavisible = false;
    },
    //弹窗渲染之前
    beforeopen() {},
    //供父组件调用函数
    openDialog(row, idCard, id) {
      this.pushTable(row.completeContent, row.servicePackageDetailRespList);
      this.completeContent = row.completeContent;
      this.param.servicePackageId = row.servicePackageId;
      this.param.serviceContractId = id;
      this.getEvaData();
      this.addDiavisible = true;
    },
    submit() {
      addComment(this.param).then((res) => {
        if (res.code == "AA000000") {
          this.$message.success(res.msg);
          this.cancel();
        }
      });
    },
    formatter1(row) {
      if (row.performanceTime != null) {
        return this.timestampToTime(row.performanceTime);
      }
    },
    transData() {},
    //时间戳转日期
    timestampToTime(timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + "-";
      var M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var D = this.appendZero(date.getDate());
      return Y + M + D;
    },
    //补0
    appendZero(obj) {
      if (obj < 10) {
        return "0" + obj;
      } else {
        return obj;
      }
    },
    //表格拼接
    pushTable(com, list) {
      this.tableData = []
      for (let key in com) {
        com[key].forEach((e, index) => {
          list.forEach((f) => {
            if ([key] == f.id) {
              this.tableData.push({
                content: f.content,
                performanceResult: index + 1 + "/" + f.yearServiceCount,
                performanceTime: e.performanceTime,
              });
            }
          });
        });
      }
    },
    //获取评价
    getEvaData() {
      getComment(this.param).then((res) => {
        if (res.code == "AA000000") {
          this.param.level = res.data.level.toString();
          this.param.content = res.data.content;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.rowSty {
  margin-top: 20px;
}
</style>